import requests
from lxml import etree
import pandas as pd
import time


# 返回substr在str中第n次出现的位置
def find_sub_tr(substr, s, n):
    count = 0
    while n > 0:
        index = s.find(substr)
        if index == -1:
            return -1
        else:
            s = s[index + 1:]
            n -= 1
            count = count + index + 1
    return count - 1


t0 = time.time()
url = 'http://10.7.7.11:8888/governance/services'
Authorization = 'Basic cm9vdDpqR3hldjJhMlBqN1cz'

headers = {'Authorization': Authorization,
           'Cookie': 'JSESSIONID=OS766KC1-9DJLVNTQZ7DSJ5CRC21W1-ADW2J7IK-5; HISTORY="com.wenwo.ads.api.account.IAdsAccountService..../governance/services/com.wenwo.ads.api.account.IAdsAccountService/providers"; tmp0=eNpdUE1PwkAQDV9CNEa9ejLEk4GmraUL3EjByIdFKUrCRbe7U1jddnG3gGj8Of5PF9SLc5iZzLx572WuD8hSSkjSewXSM6tExAbmLMQhNugyDIUhYcZUKjeGXsUiMaiIMUuMLf6rtNQ5wTGUFliptZA0LwWHIpGAUyGLkOCQA81vIfsUFlimsdYqgKbghcVcJFDUainmL3tcEMzhWBOuGIFbyVaMwwyyjJbEAuSW7%2BivaVEqQal8zJQ6%2BX%2BhcoyqHwtAS7GgLGJAc4lYP2l3IjXPiOvUQ5tQx4bIorUGiiKzHtG6E4bIIXCZGfnjHdTXkX2fZy50vXo9fcYrbCxTxo2WlHijzndloN%2BzQ%2FiHQScIukP%2F8WbY7gzMyQejzfIwQK7b96xqo90bPPjjuylqB72aN%2FJsa2JVW%2B2J3UPdfrVWrpC0abmm27BNF9Us161g8juoI%2BTYyKnEb03zc%2FoNCgeQ9Q%3D%3D',
           'User-Agent': 'PostmanRuntime/7.26.8', 'Accept': '*/*', 'Connection': 'keep-alive'}
res = requests.get(url, headers=headers, timeout=30)
# 转化html
html_str1 = etree.HTML(res.text)
# xpath提取标签下td的内容
service_names = html_str1.xpath(u"//table//tr//td//input[@value]")
# print(len(list(service_names)))
# # 所有服务的名称
# for name in service_names:
#     print(dict(name.attrib))
#     print(dict(name.attrib)['value'])
#     print()

hrefs = html_str1.xpath(u"//body//div//div//div//table//tr//td//a[@href]")
# print(len(list(hrefs)))
# # 所有服务的href
# for href in hrefs:
#     print(href.attrib)  # 获取a标签的href属性
#     print(href.text)  # 获取a标签的文本
#     print()

statuses = html_str1.xpath(u"//body//div//div//div//table//tr//td//font[@color]")
# print(len(list(statuses)))
# # 所有服务的状态
# for status in statuses:
#     print(status.attrib)  # 获取font标签的属性color值
#     print(status.text)  # 获取font标签的text文本值
#     print()

ok_service_list = []
for i in range(len(list(service_names))):
    service_name = dict(service_names[i].attrib)['value']
    service_href = dict(hrefs[i].attrib)['href']
    service_status = statuses[i].text
    if service_status == 'ok':
        if '/' in service_name:
            service_name = service_name[find_sub_tr('/', service_name, 1) + 1:]
        ok_service_list.append({'service_name': service_name,
                                'service_href': 'http://10.7.7.11:8888/governance/' + service_href,
                                'service_status': service_status})

total_ok_service_count = len(ok_service_list)
print('get ok service successfully!!! total:%d' % total_ok_service_count)
t = time.time()
df1 = pd.DataFrame(ok_service_list)
print(df1.head())
df1.to_excel("C:\\Users\\Administrator\\Desktop\\Dubbo所有OK服务.xlsx", freeze_panes=(1, 1))
print('所有OK服务excel导出成功 耗时：%d' % (t - t0))

# headers['Cookie'] = 'JSESSIONID=OS766KC1-9DJLVNTQZ7DSJ5CRC21W1-ADW2J7IK-5; HISTORY="com.wenwo.ads.api.account.IAdsAccountService..../governance/services/com.wenwo.ads.api.account.IAdsAccountService/providers"; tmp0=eNpdUE1PwkAQDV9CNEa9ejLEk4GmraUL3EjByIdFKUrCRbe7U1jddnG3gGj8Of5PF9SLc5iZzLx572WuD8hSSkjSewXSM6tExAbmLMQhNugyDIUhYcZUKjeGXsUiMaiIMUuMLf6rtNQ5wTGUFliptZA0LwWHIpGAUyGLkOCQA81vIfsUFlimsdYqgKbghcVcJFDUainmL3tcEMzhWBOuGIFbyVaMwwyyjJbEAuSW7%2BivaVEqQal8zJQ6%2BX%2BhcoyqHwtAS7GgLGJAc4lYP2l3IjXPiOvUQ5tQx4bIorUGiiKzHtG6E4bIIXCZGfnjHdTXkX2fZy50vXo9fcYrbCxTxo2WlHijzndloN%2BzQ%2FiHQScIukP%2F8WbY7gzMyQejzfIwQK7b96xqo90bPPjjuylqB72aN%2FJsa2JVW%2B2J3UPdfrVWrpC0abmm27BNF9Us161g8juoI%2BTYyKnEb03zc%2FoNCgeQ9Q%3D%3D'
# headers['User-Agent'] = 'PostmanRuntime/7.26.8'
# headers['Accept'] = '*/*'
# headers['Connection'] = 'keep-alive'
# print(headers)
# res = requests.get('http://10.7.7.11:8888/governance/services/com.wenwo.ads.api.account.IAdsAccountService/providers',
#                    headers=headers, timeout=30)
# print(res.text)
# print('OK')
services = []
for service in ok_service_list:
    current_url = service['service_href']
    res = requests.get(current_url, headers=headers, timeout=30)
    # 转化html
    html_str2 = etree.HTML(res.text)
    # xpath提取标签下td的内容
    addresses = html_str2.xpath(u"//body//div//div//div//div//table//tr//td//a[@href]")
    address_count = len(list(addresses))
    if address_count <= 0:
        continue
    for address in addresses:
        address_href = dict(address.attrib)['href']
        if 'providers' not in address_href:
            continue
        relative_href = address_href[9:find_sub_tr('/', address_href, 2)]
        new_current_url = current_url + relative_href
        res = requests.get(new_current_url, headers=headers, timeout=30)
        # 转化html
        html_str3 = etree.HTML(res.text)
        # xpath提取标签下td的内容
        # provider_info_keys = html_str3.xpath(u"//body//div//div//div//table//tr//th")
        provider_info_values = html_str3.xpath(u"//body//div//div//div//table//tr//td")

        for i in range(len(list(provider_info_values))):
            value = provider_info_values[i].text
            if i == 5:
                if value is not None and value != '':
                    methods = value.split(',')
                    for method in methods:
                        services.append({
                            '服务接口全路径': service['service_name'],
                            'Dubbo暴露地址': current_url,
                            '服务状态': service['service_status'],
                            '方法名称': method
                        })
        continue

df = pd.DataFrame(services)
print(df.shape[0])
print(df.head())
df.to_excel('C:\\Users\\Administrator\\Desktop\\Dubbo服务详情表.xlsx', freeze_panes=(1, 1))
t1 = time.time()
print('OK 总耗时：%d' % (t1 - t0))
